unit IndexTreeCtrl;

interface
uses
  SysUtils, forms, Menus, ComCtrls, buttons, classes, controls, patterns,
  IndexTreeMdl, IndexTreeView;

type
  TControllerIndexTree = class(TController)
    model: TModelIndexTree;
    view: TViewIndexTree;
  protected
    procedure DoCommand(ACommand: string; const args: string = ''); override;
  public
    constructor Create;
    destructor Destroy; override;
    procedure OnClick(Sender: TObject); //for ieventListener
    procedure OnMouseEvent(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  end;

implementation
uses
  commandlist;

procedure TControllerIndexTree.OnClick(Sender: TObject);
begin
  if Sender is TMenuItem then
  begin
    if TMenuItem(Sender).caption = '&Property' then
      Application.MessageBox('Menu Property clicked!', 'Info', 0)
    else if TMenuItem(Sender).caption = '&Refresh' then
      MOdel.refresh;
  end;

  if Sender is TTreeView then
  begin
    if assigned(TTreeView(Sender).Selected) then
      //send the node as a parameter of a String command,
      //who will handle this command?
      //I don't know and don't care eighter
      self.SendCommand(CMD_TREEINDEX_SELECT, Model.GetNodeByParam(TTreeView(Sender).Selected.Data));
  end;
end;

procedure TControllerIndexTree.OnMouseEvent(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin

end;

constructor TControllerIndexTree.Create;
begin
  inherited;
  model := TModelIndexTree.Create;
  view := TViewIndexTree.create(nil);
  view.setEventHandler(OnClick);
  model.RegObserver(view);
end;

destructor TControllerIndexTree.destroy;
begin
  freeAndNil(model);
  freeAndNil(view);
  inherited;
end;

procedure TControllerIndexTree.DoCommand(ACommand: string; const args: string = '');
begin
  if ACommand = CMD_REQUIRE_INDEXTREE then
  begin
    //make sure the view not released by the command object
    self.model.MakeIndexTree;
    self.SendCommand(TCommand.Create(CMD_SHOW_INDEXTREE, nil, view, '', nil, false));
  end;
end;

initialization
  ControlCenter.RegController(TControllerIndexTree.Create);

end.

